home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 116 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  72 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.kei.com!wang!news
  3. From: emild@cs.technion.ac.il (Kohn Emil Dan)
  4. Subject: Re: What's so different about 2-D arrays??? 
  5. Organization: Technion, Israel Institute of Technology
  6. Date: Tue, 2 Jan 1996 17:57:32 GMT
  7. Message-ID: <Pine.SV4.3.91-heb-2.04.960102194523.6304A-100000@cs.technion.ac.il>
  8. References: <820451938.20697@fredblog.demon.co.uk> 
  9. Sender: news@wang.com
  10.  
  11.  
  12.  
  13. On Sun, 31 Dec 1995, Mark Winfield wrote:
  14.  
  15. > I have been teaching myself 'C' over the last month.  Just before Xmas
  16. > I thought that I had it worked out and that all I had to do was
  17. > practice.  I decided to write a program that would record chess games,
  18. > it would allow move branching as well.  I decided upon this because it
  19. > should use everything I have learnt except DOS calls.
  20. > Now for the problem,
  21. > //My own chess recorder program
  22. > #include <stdio.h>
  23. > #include <ctype.h>
  24. > /*Prototypes*/
  25. > void setup(char pos[8][8]);   
  26. > void main()
  27. > {
  28. > char pos[8][8];
  29. >     setup(pos);
  30. >     printf("\nWell Done!!");
  31. > }
  32. > void setup(char pos[8][8])
  33. > {
  34. > int x,y;    
  35. >     pos[1][8]=pos[8][8]='r';
  36.                ^      ^  ^-------------array index to high
  37. >     for(x=1;x<9;x++){
  38. >         pos[x][7]='a';
  39.                     ^--------------x will reach the value 8, again
  40.                                      array index to high
  41. >     }
  42.  
  43. Your problem is caused by using  array subscripts that exceed the maximum 
  44. value they can get, whwn referencing an array element.
  45.  
  46. In C, array subscripts (no matter how many dimensions they have), range 
  47. from 0 to  maximum_size -1, so in your problem, having an array
  48.  
  49. char pos[8][8];
  50.  
  51. valid array references are:
  52.  
  53. pos [0][0] .. pos [0][7],
  54. pos [1][0] .. pos [1][7],
  55. ....
  56. pos [7][0] .. pos [7][7]
  57.  
  58.                         Best regards, 
  59.  
  60.                                 Emil
  61.